<template>
  <a-upload
    v-model:file-list="fileList"
    name="file"
    :action="uploadUrl"
    :headers="headers"
    :max-count="1"
    :showUploadList="false"
    @change="handleChange"
  >
    <slot></slot>
  </a-upload>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import { message, type UploadChangeParam } from 'ant-design-vue'
const baseUrl = import.meta.env.VITE_API_PREFIX
const emit = defineEmits(['upload-success'])
console.log(baseUrl, 'baseUrl')
const uploadUrl = `${baseUrl}/system/basic/postUploadOneFile`
const fileList = ref([])
const headers = ref({
  Authorization: 'Bearer <your-token>',
})

const handleChange = (info: UploadChangeParam) => {
  const { status, response, name } = info.file
  if (status === 'done') {
    message.success(`${info.file.name} file uploaded successfully`)
    if (response?.code == 1) {
      emit('upload-success', { path: response?.path, name })
    } else {
      message.error(response?.msg || 'Upload failed')
    }
  }
}
</script>
